<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-18
 * Time: 下午7:46
 */

namespace Actions;

use Data\Retreat_food_DAO;
use Data\Order_form_DAO;
use Data\Order_for_foods_DAO;
use Data\Food_DAO;

class RestaurantRetreatFoodConfirm extends BaseAction
{
    public function doAction() {
        // 退菜id
        $retreat_food_id = (int)$this->data[RETREAT_FOOD_ID];
        // 退菜记录
        $retreat_food_record = Retreat_food_DAO::getRecord($retreat_food_id);
        if (!$retreat_food_record) {
            return $this->SendErrMsg("没有此退菜记录");
        }
        // 订单id
        $order_form_id = $retreat_food_record->getORDER_FORM_ID();
        // 退菜id
        $food_id = $retreat_food_record->getFOOD_ID();
        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);
        if (!$order_form_record) {
            return $this->SendErrMsg("错误，没有此订单信息");
        }
        // 商家id
        $restaurant_id = $order_form_record->getRESTAURANT_ID();
        if ($restaurant_id != $_SESSION[RESTAURANT_ID]) {
            return $this->SendErrMsg("错误，无法确认退单");
        }
        // 是否已付款
        $order_form_pay_status = $order_form_record->getPAY_STATUS();
        if(!$order_form_pay_status) {
            return $this->SendErrMsg("订单未付款，不能退款");
        }
        // 订单内商品记录
        $order_food_record = Order_for_foods_DAO::getRecordByFormIdFoodId($order_form_id,$food_id);
        if (!$order_food_record) {
            return $this->SendErrMsg("未点过此商品，不能退款");
        }
        // 商品是否已付款
        $food_pay_status = $order_food_record->getPAY_STATUS();
        if (!$food_pay_status) {
            return $this->SendErrMsg("此商品未付款，不能退款");
        }

        // 订单内该商品改为退单
        $order_food_record->setPAY_STATUS(STATE_NO);
        Order_for_foods_DAO::update($order_food_record);

        // 订单金额修改
        // 商品金额
        $food_price = $order_food_record->getFOOD_PRICE();
        // 商品数量
        $food_count = $order_food_record->getFOOD_COUNT();
        // 商品总价
        $food_price_total = $food_count*$food_price;
        // 订单外带状态
        $is_take_out = $order_form_record->getIS_TAKE_OUT();
        // 订单所需餐盒数量
        $lunchbox_total_amounts = $order_form_record->getLUNCHBOX_TOTAL_AMOUNTS();
        // 订单内餐盒价格
        $lunchbox_total_price = $order_form_record->getLUNCHBOX_TOTAL_PRICE();
        // 订单价格
        $total_price = $order_form_record->getTOTAL_PRICE();

        if ($is_take_out==STATE_YES) {
            // 商品记录
            $food_record = Food_DAO::getRecord($food_id);
            // 商品所需餐盒
            $lunchbox_amounts = $food_record->getLUNCHBOX_AMOUNTS();
            // 商品餐盒价格
            $lunchbox_price = $food_record->getLUNCHBOX_PRICE();
            // 订单内该商品所需餐盒数量 餐盒价格
            $lunchbox_amounts = $food_count*$lunchbox_amounts;
            $lunchbox_price = $food_count*$lunchbox_price;

            // 商品总价
            $food_price_total += $lunchbox_price;
            // 订单所需餐盒数量
            $lunchbox_total_amounts -= $lunchbox_amounts;
            // 订单内餐盒价格
            $lunchbox_total_price -= $lunchbox_price;
            // 订单价格
            $total_price -= $food_price_total;
        }

        // 更新订单信息
        $order_form_record->setLUNCHBOX_TOTAL_AMOUNTS($lunchbox_total_amounts);
        $order_form_record->setLUNCHBOX_TOTAL_PRICE($lunchbox_total_price);
        $order_form_record->setTOTAL_PRICE($total_price);
        Order_form_DAO::update($order_form_record);


        // 退菜状态改为已确认
        $retreat_food_record->setRETREAT_STATUS(STATE_YES);
        Retreat_food_DAO::update($retreat_food_record);
        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}